package com.chenfan.mcn.enums;

import com.chenfan.common.extension.dict.DictTranslator;
import lombok.Getter;

import java.util.Objects;

/**
 * 通用的单据状态，特别的自己定制，比如：收入合同/执行单/红人返点合同等
 *
 * @Author liuyang
 * @Date 2021/9/29
 * @Time 11:23
 * @Version V1.0
 **/
public enum ApprovalStatusEnum implements DictTranslator<Integer, String> {

    /**
     * 审批状态
     */
    UN_COMMIT(0, "待提交"),
    APPROVAL(1, "审批中"),
    PASSED(2, "审批通过"),
    REJECTED(3, "审批拒绝"),
    CANCEL(4, "已撤回"),
    INVALID(9, "已作废");

    @Getter
    private final int code;

    @Getter
    private final String statusName;

    ApprovalStatusEnum(int code, String statusName) {
        this.code = code;
        this.statusName = statusName;
    }

    public int code() {
        return this.code;
    }

    public static ApprovalStatusEnum toEnum(Integer code) {
        if(Objects.isNull(code)) {
            return null;
        }
        ApprovalStatusEnum approvalStatusEnum = null;
        for (ApprovalStatusEnum item : ApprovalStatusEnum.values()) {
            if (item.getCode() == code) {
                approvalStatusEnum = item;
                break;
            }
        }
        return approvalStatusEnum;
    }

    public static String getStatusName(Integer code) {
        ApprovalStatusEnum approvalStatusEnum = toEnum(code);
        if(Objects.isNull(approvalStatusEnum)) {
            return null;
        }
        return approvalStatusEnum.statusName;
    }


    /**
     * 是审批通过
     * @param code
     * @return
     */
    public static boolean isPassed(Integer code) {
        return code != null && code.intValue() == ApprovalStatusEnum.PASSED.getCode();
    }

    /**
     * 是审批拒绝
     * @param code
     * @return
     */
    public static boolean isRejected(Integer code) {
        return code != null && code.intValue() == ApprovalStatusEnum.REJECTED.getCode();
    }


    /**
     * 是审批中
     * @param code
     * @return
     */
    public static boolean isApproval(Integer code) {
        return code != null && code.intValue() == ApprovalStatusEnum.APPROVAL.getCode();
    }

    /**
     * 是待提交
     * @param code
     * @return
     */
    public static boolean isUnCommit(Integer code) {
        return code != null && code.intValue() == ApprovalStatusEnum.UN_COMMIT.getCode();
    }


    /**
     * 是作废
     * @param code
     * @return
     */
    public static boolean isInvalid(Integer code) {
        return code != null && code.intValue() == ApprovalStatusEnum.INVALID.getCode();
    }

    /**
     * 是撤回
     * @param code
     * @return
     */
    public static boolean isCancel(Integer code) {
        return code != null && code.intValue() == ApprovalStatusEnum.CANCEL.getCode();
    }

    /** 根据状态码任意匹配
     * @param code
     * @param items
     * @return
     */
    public static boolean anyMatch(Integer code, ApprovalStatusEnum... items) {
        boolean match = false;
        if(code != null) {
            for(ApprovalStatusEnum item : items) {
                if(item.getCode() == code.intValue()) {
                    match = true;
                    break;
                }
            }
        }
        return match;
    }

    @Override
    public String translate(Integer code) {
        return getStatusName(code);
    }

    /**
     * 获取actionName
     * @param code
     * @return
     */
    public static String getActionName(Integer code) {
        if(Objects.isNull(code)) {
            return null;
        }
        switch (code.intValue()) {
            case 1:
                return "提交审批";
            case 4:
                return "撤回";
            case 9:
                return "作废";
            default:
                return null;
        }
    }
}
